import UMe from "../frameSccript/system/OverallSystem ";

/**
 * 坐标工具
 */
export class PosTool {
    /**
         * 转换一个节点坐标至另一个节点空间
         * @param rootNode 源节点
         * @param targetNode 目标节点
         * @returns 源节点的坐标在目标节点的坐标
         */
    rootPosToTargetNodeSpace(rootNode: cc.Node, targetNode: cc.Node): cc.Vec2 {
        let rootWrldPos = rootNode.convertToWorldSpaceAR(cc.v2(0,0));
        return targetNode.convertToNodeSpaceAR(rootWrldPos);
    }

    /**
     * 两个向量之间的距离
     * @param pos 坐标1
     * @param overPos 坐标2
     * @returns 距离
     */
    distancesBetweenTwoPostion(pos: cc.Vec2, overPos: cc.Vec2) {
        return pos.sub(overPos).mag();
    }

    /** 屏幕范围 */
    screenScope(camara: cc.Camera) {
        let pos = camara.node.convertToWorldSpaceAR(cc.v2(0,0));
        let width = UMe.Canvas().width / camara.zoomRatio;
        let height = UMe.Canvas().height / camara.zoomRatio;

        return cc.rect(pos.x, pos.y, width, height);
    }

    /** 判断一个坐标是否在屏幕内 */
    inTheScreen(camara: cc.Camera, worldPos: cc.Vec2) {
        let screen = this.screenScope(camara);

        return worldPos.x <= screen.x + screen.width / 2
            && worldPos.x >= screen.x - screen.width / 2
            && worldPos.y <= screen.y + screen.height / 2
            && worldPos.y >= screen.y - screen.height / 2;
    }

    /**
     * 获取节点世界坐标
     * @param node 目标节点
     * @returns 节点世界坐标
     */
    getNodeWordPos(node: cc.Node) {
        return node.convertToWorldSpaceAR(cc.v2(0,0));
    }
}